import Toast from "./Toast.vue";

const ToastPlugin = {
  install(Vue) {
    // 添加全局方法或属性
    Vue.prototype.$toast = {
      show(message, duration = 2000) {
        const ToastConstructor = Vue.extend(Toast);

        const toastDom = new ToastConstructor({
          el: document.createElement("div"),
          data() {
            return {
              message,
              showToast: true
            };
          }
        });

        document.body.appendChild(toastDom.$el);

        setTimeout(() => {
          toastDom.showToast = false;
          document.body.removeChild(toastDom.$el);
        }, duration);
      }
    };
  }
};

export default ToastPlugin;
